home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / olight.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  230 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. /*----------------------------------------------------------------------------
  41.  *
  42.  * olight.c : openGL (motif) example showing how to do hardware lighting
  43.  *            including two_sided lighting.
  44.  *
  45.  * Author : Yusuf Attarwala
  46.  *          SGI - Applications
  47.  * Date   : Mar 93
  48.  *
  49.  *    press  left   button for animation
  50.  *           middle button for two sided lighting (default)
  51.  *           right  button for single sided lighting
  52.  *
  53.  *
  54.  *---------------------------------------------------------------------------*/
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58.  
  59. #include <GL/glut.h>
  60.  
  61. /* function declarations */
  62.  
  63. void 
  64.     drawScene(void),
  65.     setMatrix(void),
  66.     initLightAndMaterial(void),
  67.     animation(void),
  68.     resize(int w, int h),
  69.     menu(int choice),
  70.     keyboard(unsigned char c, int x, int y);
  71.  
  72. /* global variables */
  73.             
  74. float         ax,ay,az;             /* angles for animation */
  75. GLUquadricObj *quadObj;             /* used in drawscene */
  76. static float lmodel_twoside[] = { GL_TRUE };
  77. static float lmodel_oneside[] = { GL_FALSE };
  78.  
  79. int 
  80. main(int argc, char** argv)
  81. {
  82.     glutInit(&argc, argv);
  83.  
  84.     quadObj = gluNewQuadric ();   /* this will be used in drawScene */
  85.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  86.     glutCreateWindow("Two-sided lighting");
  87.  
  88.     ax = 10.0;
  89.     ay = -10.0;
  90.     az = 0.0;
  91.  
  92.     initLightAndMaterial();
  93.  
  94.     glutDisplayFunc(drawScene);
  95.     glutReshapeFunc(resize);
  96.     glutCreateMenu(menu);
  97.     glutAddMenuEntry("Motion", 3);
  98.     glutAddMenuEntry("Two-sided lighting", 1);
  99.     glutAddMenuEntry("One-sided lighting", 2);
  100.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  101.     glutKeyboardFunc(keyboard);
  102.     glutMainLoop();
  103.     return 0;             /* ANSI C requires main to return int. */
  104. }
  105.  
  106. void
  107. drawScene(void)
  108. {
  109.     glClearColor(0.0, 0.0, 0.0, 0.0);
  110.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  111.  
  112.     glPushMatrix();
  113.     gluQuadricDrawStyle (quadObj, GLU_FILL);
  114.     glColor3f (1.0, 1.0, 0.0);
  115.     glRotatef (ax,1.0,0.0,0.0);
  116.     glRotatef (-ay,0.0, 1.0, 0.0);
  117.  
  118.     gluCylinder (quadObj, 2.0,5.0,10.0,20,8);  /* draw a cone */
  119.  
  120.     glPopMatrix();
  121.  
  122.     glutSwapBuffers();
  123. }
  124.  
  125. void
  126. setMatrix(void)
  127. {
  128.     glMatrixMode(GL_PROJECTION);
  129.     glLoadIdentity();
  130.     glOrtho(-15.0,15.0,-15.0,15.0,-10.0,10.0);
  131.     glMatrixMode(GL_MODELVIEW);
  132.     glLoadIdentity();
  133. }
  134.  
  135. int count = 0;
  136.  
  137. void
  138. animation(void)
  139. {
  140.         ax += 5.0;
  141.         ay -= 2.0;
  142.         az += 5.0;
  143.         if (ax >= 360)  ax = 0.0;
  144.         if (ay <= -360) ay = 0.0;
  145.         if (az >= 360)  az = 0.0;
  146.         drawScene();
  147.    count++;
  148.    if(count >= 60) glutIdleFunc(NULL);
  149. }
  150.  
  151. void
  152. keyboard(unsigned char c, int x, int y)
  153. {
  154.     switch(c) {
  155.         case 27 :
  156.             exit(0);
  157.             break;
  158.     default:
  159.         break;
  160.     }
  161. }
  162.  
  163. void
  164. menu(int choice)
  165. {
  166.         switch (choice) {
  167.         case 3:
  168.             count = 0;
  169.             glutIdleFunc(animation);
  170.             break;
  171.         case 2 :
  172.             glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
  173.             glutSetWindowTitle("One-sided lighting");
  174.             glutPostRedisplay();
  175.             break;
  176.         case 1 :
  177.             glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  178.             glutSetWindowTitle("Two-sided lighting");
  179.             glutPostRedisplay();
  180.             break;
  181.         }
  182. }
  183.  
  184. void 
  185. resize(int w, int h)
  186. {
  187.     glViewport(0, 0, w, h);
  188.     setMatrix();
  189. }
  190.  
  191. void 
  192. initLightAndMaterial(void)
  193. {
  194.     static float ambient[] = { 0.1, 0.1, 0.1, 1.0 };
  195.     static float diffuse[] = { 0.5, 1.0, 1.0, 1.0 };
  196.     static float position[] = { 90.0, 90.0, 150.0, 0.0 };
  197.  
  198.     static float front_mat_shininess[] = { 60.0 };
  199.     static float front_mat_specular[] = { 0.2, 0.2, 0.2, 1.0 };
  200.     static float front_mat_diffuse[] = { 0.5, 0.5, 0.28, 1.0 };
  201.     static float back_mat_shininess[] = { 60.0 };
  202.     static float back_mat_specular[] = { 0.5, 0.5, 0.2, 1.0 };
  203.     static float back_mat_diffuse[] = { 1.0, 0.9, 0.2, 1.0 };
  204.  
  205.     static float lmodel_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
  206.  
  207.     setMatrix();
  208.     glEnable(GL_DEPTH_TEST);
  209.     glDepthFunc(GL_LEQUAL);
  210.  
  211.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  212.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  213.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  214.  
  215.     glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
  216.     glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
  217.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  218.     glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
  219.     glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  220.     glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  221.  
  222.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  223.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  224.  
  225.     glEnable(GL_LIGHTING);
  226.     glEnable(GL_LIGHT0);
  227.     glShadeModel(GL_SMOOTH);
  228. }
  229.  
  230.